import { ref } from 'vue';
import { getBasXueYeChanPinFeiYongMingCheng, getXueYePinZhongInfo, getOrgName, getBasXueYeLaiYuan } from '/@/stores/baseInfo/';

/* 异步获取单个基础资料, 用法
const { mingCheng } = useBasEntity("BasXueYeLaiYuan", () => props.xueYe.laiYuanId)
*/
export const useBasEntity = <TO extends object = BasEntityType>(entityName: string, idRef) => {
	const obj = ref<TO>({ mingCheng: '', } as TO)
	const mingCheng = ref('' as string);

	// 读取名称
	const getMingCheng = async () => {
		let id = toValue(idRef)
		if (!id) {
			return;
		}
		if (!entityName) {
			mingCheng.value = id;
			return;
		}
		switch (entityName) {
			case 'SysOrg':
				mingCheng.value = getOrgName(id as number);
				break;
			case 'BasXueYePinZhong': // 血液品种
				obj.value = await getXueYePinZhongInfo(id as number);
				break;
			case 'BasXueYeLaiYuan': // 血液来源
				// 由于类型不匹配，需要进行类型断言
				obj.value = await getBasXueYeLaiYuan(id as number);
				break;
			case 'BasXueYeChanPinFeiYong': //产品收费名称
				mingCheng.value = getBasXueYeChanPinFeiYongMingCheng(id as number);
				break;
		}

		if (undefined == obj.value || null == obj.value || '' == obj.value) {
			mingCheng.value = id;
		} else {
			mingCheng.value = obj.value.mingCheng;
		}
	}

	watchEffect(() => {
		getMingCheng()
	})

	return {
		obj,
		mingCheng,
	};
};
